package org.chinacloud.wanghui.jdownload;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;

/**
 * Created with IntelliJ IDEA.
 * User: wanghui
 * Date: 13-10-30
 * Time: 下午3:13
 * To change this template use File | Settings | File Templates.
 */
public class DownloadManager extends JFrame implements Observer {

    private JTextField addTextField;
    private DownloadsTableModel tableModel;
    private JTable table;
    private JButton pauseButton,resumeButton;
    private JButton cancelButton,clearButton;

    private Download selectDownload;
    private boolean clearing;
    public DownloadManager(){
        setTitle("下载管理器");
        setSize(640,480);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                actionExit();
            }
        });
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new  JMenu("文件");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        JMenuItem fileExitMenuItem = new JMenuItem("退出",KeyEvent.VK_X);
        fileExitMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionExit();
            }
        });
        fileMenu.add(fileExitMenuItem);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);

        JPanel addPanel = new JPanel();
        addTextField = new JTextField(30);
        addPanel.add(addTextField);
        JButton addButton = new JButton("新建任务");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionAdd();
            }
        });

        addPanel.add(addButton);
        tableModel = new DownloadsTableModel();
        table = new JTable(tableModel);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                tableSelectionChanged();
            }
        });

        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        ProgressRenderer renderer = new ProgressRenderer(0,100);
        renderer.setStringPainted(true);
        table.setDefaultRenderer(JProgressBar.class,renderer);

        table.setRowHeight((int)renderer.getPreferredSize().getHeight());

        JPanel downloadsPanel = new JPanel();
        downloadsPanel.setBorder(BorderFactory.createTitledBorder("任务列表"));
        downloadsPanel.setLayout(new BorderLayout());
        downloadsPanel.add(new JScrollPane(table),BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        pauseButton = new JButton("暂停");
        pauseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionPause();
            }
        });

        pauseButton.setEnabled(false);
        buttonPanel.add(pauseButton);
        resumeButton = new JButton("恢复");
        resumeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionResume();
            }
        });

        resumeButton.setEnabled(false);
        buttonPanel.add(resumeButton);
        cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionCancel();
            }
        });

        cancelButton.setEnabled(false);
        buttonPanel.add(cancelButton);
        clearButton = new JButton("清除");
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionClear();
            }
        });
        clearButton.setEnabled(false);
        buttonPanel.add(clearButton);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(addPanel,BorderLayout.NORTH);
        getContentPane().add(downloadsPanel,BorderLayout.CENTER);
        getContentPane().add(buttonPanel,BorderLayout.SOUTH);

    }



    private void actionExit(){
        System.exit(0);
    }
    private void actionAdd(){
        URL verifiedUrl = verifyUrl(addTextField.getText());
        if(verifiedUrl != null){
            tableModel.addDownload(new Download(verifiedUrl));
            addTextField.setText("");
        }else{
            JOptionPane.showMessageDialog(this,"Invalid Download URL","Error",JOptionPane.ERROR_MESSAGE);
        }
    }


    private URL verifyUrl(String url){
        if(!url.toLowerCase().startsWith("http://"))
            return null;
        URL verifiedUrl = null;
        try{
            verifiedUrl = new URL(url);
        }catch(Exception e){
            return null;
        }
        if(verifiedUrl.getFile().length() < 2)
            return null;

        return verifiedUrl;

    }



    private void tableSelectionChanged(){
        if(selectDownload != null)
            selectDownload.deleteObserver(DownloadManager.this);

        if(!clearing){
            selectDownload =  tableModel.getDownload(table.getSelectedRow());
            selectDownload.addObserver(DownloadManager.this);
            updateButtons();
        }
    }



    private void actionPause(){
        selectDownload.pause();
        updateButtons();
    }
    private void actionResume(){
        selectDownload.resume();
        updateButtons();
    }

    private void actionCancel(){
        selectDownload.cancel();
        updateButtons();
    }

    private void actionClear(){
        clearing = true;
        tableModel.clearDownload(table.getSelectedRow());
        clearing = false;
        selectDownload = null;
        updateButtons();
    }

    private void updateButtons(){
        if(selectDownload != null){
            Status  status = selectDownload.getStatus();
            switch (status){
                case DOWNLOADING:
                    pauseButton.setEnabled(true);
                    resumeButton.setEnabled(false);
                    cancelButton.setEnabled(true);
                    clearButton.setEnabled(false);
                    break;
                case PAUSED:
                    pauseButton.setEnabled(false);
                    resumeButton.setEnabled(true);
                    cancelButton.setEnabled(true);
                    clearButton.setEnabled(false);
                    break;
                case ERROR:
                    pauseButton.setEnabled(false);
                    resumeButton.setEnabled(true);
                    cancelButton.setEnabled(false);
                    clearButton.setEnabled(true);
                    break;
                default:
                    pauseButton.setEnabled(false);
                    resumeButton.setEnabled(false);
                    cancelButton.setEnabled(false);
                    clearButton.setEnabled(true);

            }
        } else {
            pauseButton.setEnabled(false);
            resumeButton.setEnabled(false);
            cancelButton.setEnabled(false);
            clearButton.setEnabled(false);
        }
    }

    public void  update(Observable o,Object arg){
        if(selectDownload != null && selectDownload.equals(o))
            updateButtons();
    }

    public static void main(String[] args) {


        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DownloadManager ui = new DownloadManager();
                ui.setVisible(true);
            }
        });
    }


}



